package renguanyu.app.auth4fx.controller.role;

import javafx.concurrent.Task;
import javafx.scene.control.CheckBox;
import renguanyu.app.auth4fx.model.role.RolePermissionDto;
import renguanyu.app.auth4fx.model.user.UserRoleDto;
import renguanyu.app.auth4fx.service.role.RolePermissionService;
import renguanyu.app.auth4fx.view.role.RoleListView;
import renguanyu.app.auth4fx.view.role.RolePermissionListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RolePermissionListQueryController extends Task<Void> {

    private RolePermissionListView rolePermissionListView;
    public RolePermissionListQueryController(RolePermissionListView rolePermissionListView){
        this.rolePermissionListView = rolePermissionListView;
    }

    public RolePermissionListQueryController(){
    }

    public void execute() {
        System.out.println("RolePermissionListQuery.execute");
        Thread thread = new Thread(this);
        thread.setDaemon(true);
        thread.start();
    }

    private List<Map<String, Object>> mapList1 = new ArrayList<>();
    private List<Map<String, Object>> mapList2 = new ArrayList<>();
    private Integer totalPages = 0;
    @Override
    protected Void call() {
        System.out.println("RolePermissionListQuery.call");
        RolePermissionDto rolePermissionDto = buildRolePermissionDto();
        mapList1 = RolePermissionService.queryUnownedPermissionList(rolePermissionDto);
        mapList2 = RolePermissionService.queryOwnedPermissionList(rolePermissionDto);
        return null;
    }

    private static RolePermissionDto buildRolePermissionDto() {
        String roleId = RoleListView.selectedRole.getId();
        RolePermissionDto rolePermissionDto = new RolePermissionDto();
        rolePermissionDto.setRoleId(roleId);
        return rolePermissionDto;
    }


    @Override
    protected void running() {
        System.out.println("RolePermissionListQuery.running");
    }

    @Override
    protected void succeeded() {
        System.out.println("RolePermissionListQuery.succeeded");

        List<CheckBox> checkBoxList = mapList1.stream().map(e -> {
            String id = e.get("id").toString();
            String name = e.get("name").toString();
            CheckBox checkBox = new CheckBox(name);
            checkBox.getProperties().put("id", id);
            checkBox.getProperties().put("name",name);
            return checkBox;
        }).toList();
        rolePermissionListView.leftCheckBoxContainer.getChildren().addAll(checkBoxList);

        List<CheckBox> checkBoxList2 = mapList2.stream().map(e -> {
            String id = e.get("id").toString();
            String permissionName = e.get("permission_name").toString();
            CheckBox checkBox = new CheckBox(permissionName);
            checkBox.getProperties().put("id", id);
            checkBox.getProperties().put("name",permissionName);
            return checkBox;
        }).toList();
        rolePermissionListView.rightCheckBoxContainer.getChildren().addAll(checkBoxList2);

    }

    @Override
    protected void failed() {
        System.out.println("RolePermissionListQuery.failed");

    }

}
